
package privatemessanger;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

/**
 * This class will manage all the design for the Type change window.
 * @author  Erez Fatal, Oren Slor, Elad Yanai, Sigal Agami, Chen Ben Shabat,
 *          Inbar Pardo, Islamm Heralla, Ziv Hadary, Oren Shalev, Ortal David.
 */
public class TypePanel extends JPanel implements ActionListener
{
    private static String LOGIN= "Submit";
    private static String CANCEL = "Cancel";
    private LogoPanel _logoPanel;
    private UserPanel _userPanel;
    private JFrame controllingFrame; //needed for dialogs
    private MainFrame _mainFrame;

    /**
     * The constractor will get the frame of the pop up window and the frame of the whole program so it will
     * be able to update its varaibles.
     * Deviding the class into 2 parts- the logo part, and the text and buttons part.
     * @param f
     * @param mainFrame
     */
    public TypePanel( JFrame f, MainFrame mainFrame)
    {
         _mainFrame = mainFrame;

        controllingFrame = f;
        _logoPanel = new LogoPanel();
        _userPanel = new UserPanel(this, _mainFrame);

         f.getContentPane().add(_logoPanel, "West");
         f.getContentPane().add(_userPanel,"Center");

         _logoPanel.setBackground(Color.white);
         _userPanel.setBackground(Color.white);
    }

    public void actionPerformed(ActionEvent e) { throw new UnsupportedOperationException("Not supported yet."); }

    /**
     * This will place the user panel (text boxes and buttons) into the frame in the right order
     */
    public class UserPanel extends JPanel implements ActionListener, KeyListener
    {
        private JTextField _Typefield;
        private TypePanel _p;
        private MainFrame _mainFrame;

        /**
         * The constractor will get the panel and the mainframe so it will be able to add its components and edit the mainframe
         * varaibles.
         * @param p
         * @param mainFrame
         */
        public UserPanel(TypePanel p, MainFrame mainFrame)
        {
            _mainFrame= mainFrame;
            _p = p;
            PaintComponent();
        }

        /**
         * Painting the components in the right order.
         */
        private void PaintComponent()
        {
              //Lay out everything.

            JLabel _linedown = new JLabel("this is a Line                                          Down:   ");
            _linedown.setForeground(Color.white);
            _p.add(_linedown);
            _linedown.setVisible(true);

            JLabel _type = new JLabel("Enter Your Type:  ");
            _p.add(_type);
            _type.setVisible(true);
            _p.setBackground(Color.white);

            _Typefield = new JTextField(12);
            _Typefield.addActionListener(this);
            _Typefield.addKeyListener(this);
            _p.add(_Typefield);
            _Typefield.setVisible(true);

            JComponent buttonPane = createButtonPanel();

            JLabel _linedown2 = new JLabel("this is a Line                                          Down:  ");
            _linedown2.setForeground(Color.white);
            _p.add(_linedown2);

            _p.add(buttonPane);
            _p.repaint();
        }

        /**
         * Creating the button panel
         * @return the button panel
         */
        protected JComponent createButtonPanel()
        {
            JPanel p = new JPanel(new GridLayout(0,2));
            JButton LoginButton = new JButton("Submit");
            JButton cancelButton = new JButton("Cancel");

            LoginButton.setActionCommand(LOGIN);
            cancelButton.setActionCommand(CANCEL);
            LoginButton.addActionListener(this);

            _mainFrame.getRootPane().setDefaultButton(LoginButton);
            cancelButton.addActionListener(this);

            p.add(LoginButton);
            p.add(cancelButton);

            return p;
        }

        /**
         * All the logic behind the action, making a validation to the input and do the wanted action.
         * @param e
         */
         public void actionPerformed(ActionEvent e)
         {
            String cmd = e.getActionCommand();

            if (LOGIN.equals(cmd))
            {
                // if the user pressed on Login
                if(_Typefield.getText().length()==0)
                {
                    JOptionPane.showMessageDialog(controllingFrame, "Invalid Type. Please Try again.","Error Message",JOptionPane.ERROR_MESSAGE);
                    return;
                }
                int isspace=_Typefield.getText().indexOf(" ");
                if(isspace != -1)
                {
                    JOptionPane.showMessageDialog(controllingFrame, "No Space Allowed in Type Name. Try again.","Error Message",JOptionPane.ERROR_MESSAGE);
                    return;
                }
                MainFrame._user.setType((_Typefield.getText()));
                _mainFrame.changeTypeSucceed();
                controllingFrame.dispose();
                _mainFrame.setVisible(true);
            }
            else
            {
                //The user has pressed on Cancel.
                controllingFrame.dispose();
                _mainFrame.setVisible(true);
            }
         }

        /**
         * When the user press ENTER key and not "confirm" with the mouse. do the same logic as the confirm with the mouse.
         * @param e
         */
        public void keyTyped(KeyEvent e)
        {
            if(e.getKeyCode()== KeyEvent.VK_ENTER)
            {
                String cmd = "Submit";
                if (LOGIN.equals(cmd))
                {
                    // if the user pressed on Submit
                    if(_Typefield.getText().length()==0)
                    {
                        JOptionPane.showMessageDialog(controllingFrame, "Invalid Type. Try again.","Error Message",JOptionPane.ERROR_MESSAGE);
                        return;
                    }
                    int isIPspace=_Typefield.getText().indexOf(" ");
                    if(isIPspace != -1)
                    {
                        JOptionPane.showMessageDialog(controllingFrame, "No Space Allowed in Type. Try again.","Error Message",JOptionPane.ERROR_MESSAGE);
                        return;
                    }

                    MainFrame._user.setType((_Typefield.getText()));
                    _mainFrame.changeTypeSucceed();
                    controllingFrame.dispose();
                    _mainFrame.setVisible(true);
                }
                else
                {
                    //The user has pressed on Cancel.
                    controllingFrame.dispose();
                    _mainFrame.setVisible(true);
                }
            }
        }

        public void keyPressed(KeyEvent e) { keyTyped(e); }

        public void keyReleased(KeyEvent e) { }
    }

    /**
     * This class will handle the logo side of the frame.
     */
    public class LogoPanel extends JPanel
    {
        private JLabel _logo;
        private JFrame _f;

        public LogoPanel()
        {
            setBackground(Color.white);
            ImageIcon _icon = createImageIcon("logo.jpg", "Messenger!");
            _logo = new JLabel(_icon);
            add(_logo);
            _logo.setVisible(true);
            repaint();
        }

        /**
         * Laying the logo into its place.
         * @param path
         * @param description
         * @return
         */
        protected ImageIcon createImageIcon(String path, String description)
        {
            java.net.URL imgURL = LogoPanel.class.getResource(path);
            if (imgURL != null)
            {
                return new ImageIcon(imgURL, description);
            }
            else
            {
                System.err.println("Couldn't find file: " + path);
                return null;
            }
        }
    }
}
